package com;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class ParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取参数
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String[] hobby=request.getParameterValues("happy");

        System.out.println("用户名："+username);
        System.out.println("密码："+password);
        System.out.println("爱好："+ Arrays.asList(hobby));


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置请求体的字符为utf-8,从而解决post请求的乱码问题
        //也要在获取请求参数之前调用才有效
        request.setCharacterEncoding("UTF-8");
        System.out.println("-----------------doPost---------------");
        //获取请求参数
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String[] hobby=request.getParameterValues("happy");
        System.out.println("用户名："+username);
        System.out.println("密码："+password);
        System.out.println("爱好："+ Arrays.asList(hobby));
    }
}
